package com.cec.controller.user;


import com.cec.dto.UserDTO;
import com.cec.dto.UserLoginDTO;
import com.cec.entity.User;
import com.cec.properties.JwtProperties;
import com.cec.result.Result;
import com.cec.service.user.UserService;
import com.cec.utils.JwtUtil;
import com.cec.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@RestController("userController")
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @ApiOperation("用户登陆")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        User user = userService.login(userLoginDTO);
        Map<String,Object> claim=new HashMap<>();
        claim.put("userId",user.getId());
        String token = JwtUtil.createToken(jwtProperties.getUserOutTime(),jwtProperties.getUserSecretKey(),claim);
        UserLoginVO userLoginVO = new UserLoginVO(userLoginDTO.getUserName(),user.getImg(),token);
        return Result.success(userLoginVO);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result registerUser(@RequestBody UserDTO userDTO){
        return userService.registerUser(userDTO);
    }

    @GetMapping
    @ApiOperation("获取当前用户信息")
    public Result getUserInfo(){
        return userService.getUserInfo();
    }

    @PostMapping
    @ApiOperation("修改用户信息")
    public Result updateUser(@RequestBody UserDTO userDTO){
        return userService.updateUser(userDTO);
    }
}

